package com.cqjtu.eecs.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cqjtu.eecs.entity.Course;
import com.cqjtu.eecs.entity.CourseSystem;
import com.cqjtu.eecs.entity.Student;
import com.cqjtu.eecs.entity.vo.CourseSystemVO;
import com.cqjtu.eecs.entity.vo.CourseTreeVO;
import com.cqjtu.eecs.entity.vo.children1;
import com.cqjtu.eecs.entity.vo.children2;
import com.cqjtu.eecs.service.ICourseService;
import com.cqjtu.eecs.service.ICourseSystemService;
import com.cqjtu.eecs.utils.MapUtils;
import com.cqjtu.eecs.utils.Result;
import com.cqjtu.eecs.utils.ResultCode;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程体系 前端控制器
 * </p>
 *
 * @author   
 * @since 2022-02-28
 */
@RestController
@RequestMapping("/course-system")
@Api(tags = "课程体系管理")
public class CourseSystemController {
    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private ICourseSystemService iCourseSystemService;
    @Autowired
    private MapUtils mapUtils;
    @Autowired
    private ICourseService iCourseService;

    @ApiOperation("增加新的课程体系")
    @PostMapping("/insert")
    public String insert(@RequestBody CourseSystem courseSystem) throws JsonProcessingException {
        boolean save = iCourseSystemService.save(courseSystem);
        if(save){
            return objectMapper.writeValueAsString(Result.success());
        }
        return objectMapper.writeValueAsString(Result.error());
    }

    @ApiOperation("根据课程体系id删除课程")
    @DeleteMapping("/deleteCourseSystemById/{id}")
    public String delete(@PathVariable("id") Integer id) throws JsonProcessingException {
        boolean flag = iCourseSystemService.removeById(id);
        if(flag){
            return objectMapper.writeValueAsString(Result.success());
        }
        return objectMapper.writeValueAsString(Result.error(-1,"该课程体系可能已经删除，不存在！"));
    }

    @ApiOperation("根据课程体系id修改课程体系")
    @PutMapping("/putCourseSystem")
    public String putCourse(@RequestBody CourseSystem courseSystem) throws JsonProcessingException {
        int i = iCourseSystemService.updateCourseByCourseSystemId(courseSystem.getCourseSystemId(), courseSystem);
        if(i==1){
            return objectMapper.writeValueAsString(Result.success());
        }
        return objectMapper.writeValueAsString(Result.error(-1,"该课程体系id可能不存在！"));
    }

    @ApiOperation("查询所有课程体系")
    @GetMapping("/selectAllCourseSystem")
    public Result selectAllCourseSystem()  {
        ArrayList<CourseSystem> courseSystems = iCourseSystemService.selectAllCourseSystem();
        Map<Integer, String> mapUtilsCourseSystemUtil = mapUtils.getCourseSystemUtil();
        List<CourseSystemVO> result=new ArrayList<>();
        for (int i=0;i<courseSystems.size();i++){
            CourseSystem temp=courseSystems.get(i);
            result.add(new CourseSystemVO(temp.getCourseSystemId(),temp.getCourseSystemName(),temp.getPreCourseSystemId(),mapUtilsCourseSystemUtil.get(temp.getPreCourseSystemId())));
        }
        return Result.success(result);
    }

    @ApiOperation("根据课程体系id获得课程体系名")
    @GetMapping("/selectcourseSystemNameBycourseSystemId/{courseSystemId}")
    public Result selectcourseSystemNameBycourseSystemId(@PathVariable("courseSystemId") Integer courseSystemId){
        CourseSystem byId = iCourseSystemService.getById(courseSystemId);
        if(byId==null) return Result.error(-1,"该课程体系id不存在！");
        return Result.success(ResultCode.SUCCESS,byId.getCourseSystemName());
    }

    @ApiOperation("根据课程体系名获得下面所有课程")
    @GetMapping("/selectAllCourseNameByCourseSystemName/{courseSystemName}")
    public Result selectAllCourseNameByCourseSystemName(@PathVariable String courseSystemName){
        List<CourseSystem> collect = iCourseSystemService.list().stream().filter(s -> s.getCourseSystemName().equals(courseSystemName)).collect(Collectors.toList());
        Integer courseSystemId = collect.get(0).getCourseSystemId();
        List<Course> collect1 = iCourseService.list().stream().filter(s -> s.getCourseSystemId().equals(courseSystemId)).collect(Collectors.toList());
        List<String> ans=new ArrayList<>();
        for (int i=0;i<collect1.size();i++){
            ans.add(collect1.get(i).getCourseName());
        }
        return Result.success(ans);
    }

    @GetMapping("/getCourseTree")
    @ApiOperation("得到课程体系与课程的树")
    public Result getCourseTree(){
        ArrayList<CourseSystem> preCourseSystems = iCourseSystemService.selectAllCourseSystemIsNull();
        List<CourseTreeVO> courseTreeVOList=new ArrayList<>();
        List<children1> children1s=null;
        for (int i=0;i<preCourseSystems.size();i++){
            Integer temp=preCourseSystems.get(i).getCourseSystemId();
            System.out.println(temp+"&&&&&&&&&&&&&&&&&");
           // List<CourseSystem> collect1234 = iCourseSystemService.list().stream().filter(s -> s.getPreCourseSystemId().equals(temp)).collect(Collectors.toList());
            List<CourseSystem> collect1234=iCourseSystemService.selectAllCourseSystemByPreCourseSystemId(temp);
            System.out.println(collect1234.size()+"---------------------");
            children1s=new ArrayList<>();
            for (int j=0;j<collect1234.size();j++){
                String sp=collect1234.get(j).getCourseSystemName();
                List<CourseSystem> collect = iCourseSystemService.list().stream().filter(s -> s.getCourseSystemName().equals(sp)).collect(Collectors.toList());
                Integer courseSystemId = collect.get(0).getCourseSystemId();
                List<Course> collect1 = iCourseService.list().stream().filter(s -> s.getCourseSystemId().equals(courseSystemId)).collect(Collectors.toList());
                List<String> data=new ArrayList<>();
                for (int ii=0;ii<collect1.size();ii++){
                    data.add(collect1.get(ii).getCourseName());
                }
                List<children2> t=new ArrayList<>();
                for (int z=0;z<data.size();z++){
                    t.add(new children2(data.get(z)));
                }

                children1s.add(new children1(collect1234.get(j).getCourseSystemName(),t));
            }
            courseTreeVOList.add(new CourseTreeVO(preCourseSystems.get(i).getCourseSystemName(),children1s));
        }
        return Result.success(courseTreeVOList);
    }
}
